package pure.ui 
{
	import pure.engine.system.SystemManager;
	import pure.ui.skins.CloseWindowSkin;
	import pure.ui.skins.ISkin;
	import flash.display.Stage;
	import flash.events.MouseEvent;
	import flash.geom.Point;
	
	/**
	 * 可拖拽窗口
	 * @author Des
	 * <皮肤种类> ---- CloseWindowSkin
	 * 
	 */
	
public class CloseWindow extends Window 
{
	
	public function CloseWindow(x:Number, y:Number, size:Point = null, titleSize:Point = null, closeSize:Point = null,skin:ISkin = null, autoTitleCenter:Boolean = false, dragging:Boolean = true) 
	{
		super(x, y, size, titleSize, skin, autoTitleCenter, dragging);
		if (closeSize)
		{
			_closeButton.setSize(closeSize.x,closeSize.y);
		}
		else
		{
			_closeButton.setSize(CLOSE_WIDTH,CLOSE_HEIGHT);
		}
	}
	

	/*default*/
	
	protected static const CLOSE_WIDTH:Number = 24;
	
	protected static const CLOSE_HEIGHT:Number = 24;
	
	/*cache*/
	
	protected var _closeButton:Button;
	
	protected var _autoDestroy:Boolean = false;	// 点击关闭按钮时是否销毁(否则隐藏)
	
	
	
	/**
	 * 毁灭
	 **/
	override public function destroy():void
	{
		super.destroy();
		_closeButton.destroy();
	}
	
	
	
	
	
	/* ----------------------------------------------------    get / set    ----------------------------------------------------*/
	
	
	
	
	
	// 可关闭窗口皮肤
	override public function set skin(value:ISkin):void
	{
		if (!value)
		{
			return;
		}
		if (!value is CloseWindowSkin)
		{
			throw new Error('CloseWindow::skin - 皮肤类型错误！');
		}
		this._skin = CloseWindowSkin(value).content;
		_title.skin = CloseWindowSkin(value).title;
		_closeButton.skin = CloseWindowSkin(value).close;
		update();
	}	
	
	// 点击关闭按钮时是否销毁(否则隐藏)
	public function set autoDestroy(b:Boolean):void
	{
		_autoDestroy = b;
	}
	
	// 关闭按钮
	public function get closeButton():Button
	{
		return _closeButton;
	}
	
	
	
	
	
	/* ----------------------------------------------------    private    ----------------------------------------------------*/
	
	
	
	
	
	/**
	 * 子元素
	 **/
	override protected function _addChildren():void
	{
		super._addChildren();
		
		// 关闭按钮
		_closeButton = new Button();
		_title.addChild(_closeButton);
		_closeButton.setObserverClick(_onClose);
	}
	
	/**
	 * 隐藏窗口
	 **/
	protected function _onClose():void
	{
		this.hide(_autoDestroy);
	}
		
	/**
	 * 绘制
	 **/
	override protected function _draw():void
	{
		super._draw();
		_rejustCloseButton();
	}
	
	/**
	 * 按住拖拽
	 **/
	override protected function ____onMouseDown(e:MouseEvent):void
	{
		// 按到关闭按钮时不拖拽
		if (e.target != e.currentTarget)
		{
			return;
		}
		super.____onMouseDown(e);
	}
	
	/**
	 * 调整关闭按钮
	 **/
	protected function _rejustCloseButton():void
	{
		var gap:Number = _title.height * 0.5;
		_closeButton.x = _title.width - gap;
		_closeButton.y = gap;
	}

}

}